export const checkSession = async () => {
  let flag = await new Promise((resolve, reject) => {
    wx.checkSession({
      success () {
        resolve(true)
      },
      fail () {
        resolve(false)
      }
    })
  })
  return flag
}

export const getSetting = async () => {
  let rs = await new Promise((resolve, reject) => {
    wx.getSetting({
      success (res) {
        if (res.errMsg === 'getSetting:ok') {
          resolve(res.authSetting)
        } else {
          reject(new Error('wx get setting error!'))
        }
      },
      fail () {
        reject(new Error('wx get setting error!'))
      }
    })
  })
  return rs
}

export const login = async () => {
  let code = await new Promise((resolve, reject) => {
    wx.login({
      success (res) {
        if (res.code) {
          resolve(res.code)
        } else {
          resolve(null)
        }
      },
      fail (e) {
        reject(e)
      }
    })
  })
  return code
}

export const getShareInfo = async (shareTicket) => {
  let rs = await new Promise((resolve, reject) => {
    wx.getShareInfo({
      shareTicket,
      success (res) {
        if (res.errMsg === 'getShareInfo:ok') {
          let {encryptedData, iv} = res
          resolve({encryptedData, iv})
        } else {
          reject(new Error('wx get share info error!'))
        }
      }
    })
  })
  return rs
}
